package evi.com.evi.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RadioGroup;


import cn.jpush.android.api.JPushInterface;
import evi.com.evi.R;
import evi.com.evi.application.MyApplication;
import evi.com.evi.fragment.DisposeFalseFragment;
import evi.com.evi.fragment.DisposeTrueFragment;
import evi.com.evi.fragment.ManagerFragment;
import evi.com.evi.fragment.SettingFragment;
import evi.com.evi.utils.ChannelStatusConsts;
import evi.com.evi.utils.FragmentTabUtils;
import evi.com.evi.utils.HeartBreakUtil;
import evi.com.evi.utils.SPUtil;
import evi.com.evi.utils.ScreenUtils;
import evi.com.evi.utils.ToastUtils;
import okhttp3.OkHttpClient;

public class HomeActivity extends FragmentActivity {
    private static final String TAG = "TAG";
    public static boolean isStart = true;
    private RadioGroup rg;
    private int fragment_container;
    private FragmentTabUtils fragmentTabUtils; // 用于管理fragment
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private Fragment fragment4;
    private static OkHttpClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        JPushInterface.setSilenceTime(this,23,59,23,59);
        if (client == null) {
            client = new OkHttpClient();
        }
        ScreenUtils.setTranStatus(this, false, Color.parseColor("#FF6600"));
//        PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, ContentUtils.API_KEY);
        setContentView(R.layout.activity_home);

        initView();

        //极光服务被停止，则重新启动极光推送
        if (JPushInterface.isPushStopped(MyApplication.getContext())) {

            String rid = JPushInterface.getRegistrationID(MyApplication.getContext());
            SPUtil.setString(MyApplication.getContext(), "channelId", rid);
            Log.d("HomeActivity", "推送服务补停止，现在resume");
            JPushInterface.resumePush(MyApplication.getContext());
        }

        //channel info上线
        HeartBreakUtil.sendHeartBreakPacket(client, MyApplication.getContext(), ChannelStatusConsts.CHANNEL_STATUS_WORK);
        //开始心跳包
        new HeartBreakUtil().startRepeatTask("YW_EVI_HEART_BREAK_EVENT");

    }

    private void initView() {
        rg = (RadioGroup) findViewById(R.id.radiogroup);
        fragment_container = R.id.framelayout_container;

//        fragment1 = new DisposeFalseFragment();
        fragment2 = new DisposeTrueFragment();
        fragment3 = new ManagerFragment();
        fragment4 = new SettingFragment();


        List<Fragment> list = new ArrayList<Fragment>();
//        list.add(fragment1);
        list.add(fragment2);
        list.add(fragment3);
        list.add(fragment4);
        fragmentTabUtils = new FragmentTabUtils(rg, fragment_container, getSupportFragmentManager(), list);
        fragmentTabUtils.show(0);
    }

    /**
     * 回退键的处理:两次退出程序
     */
    private long lastExitTime = 0;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - lastExitTime) > 2000) {
                lastExitTime = System.currentTimeMillis();// 第一次按
                ToastUtils.showMsg(HomeActivity.this, "再按一次退出程序");
            } else {
                new HeartBreakUtil().stopRepeatTask("YW_EVI_HEART_BREAK_EVENT");
                HeartBreakUtil.sendHeartBreakPacket(client, MyApplication.getContext(), ChannelStatusConsts.CHANNEL_STATUS_REST);
                MyApplication.getInstance().exitAllActivity();
//                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onStart() {
        super.onStart();
        isStart = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        isStart = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().deleteActivity(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int tag = intent.getIntExtra("tag", 0);
//        showOne(tag);
    }

//    public void showOne(int tag) {
//        if (tag == 2) {
//            ((DisposeFalseFragment) fragment1).restart(tag);
//            fragmentTabUtils.show(0);
//        }
//    }

}
